﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="SequenceArrayUserDefinedSTypeReferenced", Namespace="http://kis.x-road.eu/", Target=typeof(SequenceArrayUserDefinedSTypeReferenced))]
    public class SequenceArrayUserDefinedSTypeReferencedSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected SequenceArrayUserDefinedSTypeReferenced clazz;
        
        public SequenceArrayUserDefinedSTypeReferencedSerializer(SequenceArrayUserDefinedSTypeReferenced clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("SimpleTypeReferenced");
            if (clazz.SimpleTypeReferenced!=null) {
                if (((clazz.SimpleTypeReferenced == null) 
                            || (clazz.SimpleTypeReferenced.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "itemNonNil");
                }
                for (int x = 0; ((clazz.SimpleTypeReferenced != null) 
                            && (x < clazz.SimpleTypeReferenced.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("itemNonNil");
                    if (clazz.SimpleTypeReferenced[x]!=null) {
                        Writer.WriteString(clazz.SimpleTypeReferenced[x].ToString());
                    }
                    else {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : SimpleTypeReferenced");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : SimpleTypeReferenced");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("SimpleTypeReferenced2");
            if (clazz.SimpleTypeReferenced2!=null) {
                if (((clazz.SimpleTypeReferenced2 == null) 
                            || (clazz.SimpleTypeReferenced2.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "itemNillable");
                }
                for (int x = 0; ((clazz.SimpleTypeReferenced2 != null) 
                            && (x < clazz.SimpleTypeReferenced2.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("itemNillable");
                    if (clazz.SimpleTypeReferenced2[x]!=null) {
                        Writer.WriteString(clazz.SimpleTypeReferenced2[x].ToString());
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : SimpleTypeReferenced2");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("SimpleTypeInline");
            if (clazz.SimpleTypeInline!=null) {
                if (((clazz.SimpleTypeInline == null) 
                            || (clazz.SimpleTypeInline.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "itemNonNil");
                }
                for (int x = 0; ((clazz.SimpleTypeInline != null) 
                            && (x < clazz.SimpleTypeInline.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("itemNonNil");
                    if (clazz.SimpleTypeInline[x]!=null) {
                        Writer.WriteString(clazz.SimpleTypeInline[x].ToString());
                    }
                    else {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : SimpleTypeInline");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : SimpleTypeInline");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("SimpleTypeInline2");
            if (clazz.SimpleTypeInline2!=null) {
                if (((clazz.SimpleTypeInline2 == null) 
                            || (clazz.SimpleTypeInline2.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "itemNil");
                }
                for (int x = 0; ((clazz.SimpleTypeInline2 != null) 
                            && (x < clazz.SimpleTypeInline2.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("itemNil");
                    if (clazz.SimpleTypeInline2[x]!=null) {
                        Writer.WriteString(clazz.SimpleTypeInline2[x].ToString());
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : SimpleTypeInline2");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="SimpleTypeReferenced") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeReferenced");
                }
                clazz.SimpleTypeReferenced = new System.Collections.Generic.List<global::Kis.Xroad.Eu.WorkDay>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("SimpleTypeReferenced");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "itemNonNil")); ) {
                        if (Reader.LocalName=="itemNonNil") {
                            if (Reader.IsElementNil()) {
                                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeReferenced>itemNonNil");
                            }
                            clazz.SimpleTypeReferenced.Add(new global::Kis.Xroad.Eu.WorkDay(Reader.ReadElementString("itemNonNil")));
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeReferenced>itemNonNil", Reader.LocalName);
                        }
                    }
                    if ((clazz.SimpleTypeReferenced.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "itemNonNil");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeReferenced", Reader.LocalName);
            }
            if (Reader.LocalName=="SimpleTypeReferenced2") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeReferenced2");
                }
                clazz.SimpleTypeReferenced2 = new System.Collections.Generic.List<global::Kis.Xroad.Eu.WorkDay>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("SimpleTypeReferenced2");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "itemNillable")); ) {
                        if (Reader.LocalName=="itemNillable") {
                            if ((Reader.IsElementNil() == false)) {
                                clazz.SimpleTypeReferenced2.Add(new global::Kis.Xroad.Eu.WorkDay(Reader.ReadElementString("itemNillable")));
                            }
                            else {
                                clazz.SimpleTypeReferenced2.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeReferenced2>itemNillable", Reader.LocalName);
                        }
                    }
                    if ((clazz.SimpleTypeReferenced2.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "itemNillable");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeReferenced2", Reader.LocalName);
            }
            if (Reader.LocalName=="SimpleTypeInline") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeInline");
                }
                clazz.SimpleTypeInline = new System.Collections.Generic.List<SequenceArrayUserDefinedSTypeReferenced.SimpleTypeInlineType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("SimpleTypeInline");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "itemNonNil")); ) {
                        if (Reader.LocalName=="itemNonNil") {
                            if (Reader.IsElementNil()) {
                                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeInline>itemNonNil");
                            }
                            clazz.SimpleTypeInline.Add(new SequenceArrayUserDefinedSTypeReferenced.SimpleTypeInlineType(Reader.ReadElementString("itemNonNil")));
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeInline>itemNonNil", Reader.LocalName);
                        }
                    }
                    if ((clazz.SimpleTypeInline.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "itemNonNil");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeInline", Reader.LocalName);
            }
            if (Reader.LocalName=="SimpleTypeInline2") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeInline2");
                }
                clazz.SimpleTypeInline2 = new System.Collections.Generic.List<SequenceArrayUserDefinedSTypeReferenced.SimpleTypeInline2Type>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("SimpleTypeInline2");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "itemNil")); ) {
                        if (Reader.LocalName=="itemNil") {
                            if ((Reader.IsElementNil() == false)) {
                                clazz.SimpleTypeInline2.Add(new SequenceArrayUserDefinedSTypeReferenced.SimpleTypeInline2Type(Reader.ReadElementString("itemNil")));
                            }
                            else {
                                clazz.SimpleTypeInline2.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeInline2>itemNil", Reader.LocalName);
                        }
                    }
                    if ((clazz.SimpleTypeInline2.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "itemNil");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SequenceArrayUserDefinedSTypeReferenced>SimpleTypeInline2", Reader.LocalName);
            }
        }
    }
}
